package cn.wolfcode.p2p.hosting.service.impl;

import cn.wolfcode.p2p.base.domain.Account;
import cn.wolfcode.p2p.base.domain.BaseAuditDomain;
import cn.wolfcode.p2p.base.service.IAccountService;
import cn.wolfcode.p2p.business.service.IAccountFlowService;
import cn.wolfcode.p2p.hosting.domain.RechargeOnline;
import cn.wolfcode.p2p.hosting.mapper.RechargeOnlineMapper;
import cn.wolfcode.p2p.hosting.service.IHostingService;
import cn.wolfcode.p2p.hosting.service.IRechargeOnlineService;
import cn.wolfcode.p2p.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

@Service
@Transactional
public class RechargeOnlineServiceImpl implements IRechargeOnlineService {

    @Autowired
    private RechargeOnlineMapper rechargeOnlineMapper;

    @Autowired
    private IHostingService hostingService;
    @Autowired
    private IAccountService accountService;
    @Autowired
    private IAccountFlowService accountFlowService;

    @Override
    public String apply(BigDecimal amount) {

        //1.保存充值记录
        RechargeOnline rechargeOnline = new RechargeOnline();
        String uuid = UUID.randomUUID().toString();
        rechargeOnline.setTradeCode(uuid);

        rechargeOnline.setAmount(amount);
        rechargeOnline.setApplier(UserContext.getCurrentUser());
        rechargeOnline.setApplyTime(new Date());
        rechargeOnlineMapper.insert(rechargeOnline);

        //2.提交托管充值接口申请
        String html = hostingService.hostingRecharge(rechargeOnline);
        return html;
    }

    @Override
    public RechargeOnline getByTradeCode(String tradeCode) {

        return rechargeOnlineMapper.getByTradeCode(tradeCode);
    }

    @Override
    public String resultHandler(Map<String, String> map) {
        //1.根据流水号获取充值记录
        String outTradeNo = map.get("out_trade_no");
        RechargeOnline re = getByTradeCode(outTradeNo);
        if (re == null) {
            return "ERROR";
        }
        //2.根据充值结果状态修改记录状态
        String depositStatus = map.get("deposit_status");
        //处理中不做任何事情,
        if (depositStatus.equals("PROCESSING")) {
            return "ERROR";
        }
        //充值成功
        if (depositStatus.equals("SUCCESS")) {
            re.setTradeTime(new Date());
            re.setState(BaseAuditDomain.STATE_SUCCESS);
            rechargeOnlineMapper.updateByPrimaryKey(re);
            //3.修改充值用户的账户金额
            Account account = accountService.getById(re.getApplier().getId());
            account.addUsableAmount(re.getAmount());
            accountService.update(account);

            //4.产生流水
            accountFlowService.createRechargeOnlineSuccessFlow(account, re.getAmount());

            return "SUCCESS";
        }
        if (depositStatus.equals("FAILED")) {
            re.setTradeTime(new Date());
            re.setState(BaseAuditDomain.STATE_REJECT);
            rechargeOnlineMapper.updateByPrimaryKey(re);
            return "SUCCESS";
        }

        return "ERROR";
    }
}
